#include "EnemyManager.h"
#include "Scene\GameScene\EnemyNode.h"

EnemyManager* EnemyManager::createManager(const char *levelPlist) {
	EnemyManager *manager = new EnemyManager();
	if (manager && manager->initManager(levelPlist)) {
		manager->autorelease();
		return manager;
	}
	CC_SAFE_RETAIN(manager);
	return nullptr;
}

EnemyManager::~EnemyManager() {
	CC_SAFE_RELEASE(m_monster);
	CC_SAFE_RELEASE(m_senior_monster);
}

bool EnemyManager::initManager(const char *levelPlist) {
	srand(time(0));
	m_data = __Dictionary::createWithContentsOfFile(levelPlist);
	m_count = m_data->valueForKey("enemy_count")->intValue();
	m_totalCount = m_count;
	m_senior_limit = m_data->valueForKey("senior_limit")->intValue();
	m_monster = dynamic_cast<__Array *>(m_data->objectForKey("monster_name"));
	CC_SAFE_RETAIN(m_monster);
	m_senior_monster = dynamic_cast<__Array *>(m_data->objectForKey("senior_monster_name"));
	CC_SAFE_RETAIN(m_senior_monster);

	return true;
}

Node* EnemyManager::getEnemyNode(Vec2 pos, Node *layer) {
	if (m_count > 0) {
		if (m_count > m_senior_limit) {
			const char *enemyType = dynamic_cast<__String *>(m_monster->getRandomObject())->getCString();
			m_count--;
			return EnemyNode::createEnemyNode(enemyType, pos, layer);
		}
		else {
			const char *enemyType = dynamic_cast<__String *>(m_senior_monster->getRandomObject())->getCString();
			m_count--;
			return EnemyNode::createEnemyNode(enemyType, pos, layer);
		}
	}
	else {
		return nullptr;
	}
}